﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TileMapTool
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        BitmapService service;

        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Load image and compute tile information
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoadImage_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "Open Map Image";
            dialog.Filter = "JPG |*.jpg|PNG|*.png|BMP|*.bmp";
            if ((bool)dialog.ShowDialog())
            {
                ImageBrush ib = new ImageBrush();
                ib.ImageSource = new BitmapImage(new Uri(dialog.FileName, UriKind.Absolute));
                cvsMap.Width = ib.ImageSource.Width;
                cvsMap.Height = ib.ImageSource.Height;
                cvsMap.Background = ib;

                int tileWidth = string.IsNullOrEmpty(txtTileWidth.Text) ? Tile.Width : int.Parse(txtTileWidth.Text);
                int tileHeight = string.IsNullOrEmpty(txtTileWidth.Text) ? Tile.Height : int.Parse(txtTileHeight.Text);

                service = new BitmapService(dialog.FileName, tileWidth, tileHeight);
                tblMapHeight.Text = service.Bitmap.Height.ToString();
                tblMapWidth.Text = service.Bitmap.Width.ToString();
                tblMapTile.Text = service.Bitmaps.Count.ToString();
                tblMapTileAfter.Text = service.Tiles.Count.ToString();

                btnCreateTile.Visibility = Visibility.Visible;
                txtTileWidth.Visibility = System.Windows.Visibility.Collapsed;
                txtTileHeight.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        /// <summary>
        /// Create Tile from image and save
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCreateTile_Click(object sender, RoutedEventArgs e)
        {
            if (service != null)
            {
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.Title = "Save Game Map";
                saveDialog.Filter = "TEXT |*.txt";
                if ((bool)saveDialog.ShowDialog())
                {
                    service.SaveTileSet(saveDialog.FileName);
                    Bitmap bitmap = service.GetTileSetBitmap();
                    bitmap.Save(saveDialog.FileName.Replace(".txt", ".png").Trim(), System.Drawing.Imaging.ImageFormat.Png);

                    txtTileWidth.Visibility = System.Windows.Visibility.Visible;
                    txtTileHeight.Visibility = System.Windows.Visibility.Visible;
                }
                

            }
        }

        /// <summary>
        /// Load tile from file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoadMap_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "Open Map Image";
            dialog.Filter = "TEXT |*.txt|PNG|*.png";
            if ((bool)dialog.ShowDialog())
            {
                service = new BitmapService();
                Bitmap bitmap = service.LoadMapFromFile(dialog.FileName, dialog.FileName.Replace(".txt", ".png"));
                //Bitmap bitmap = new Bitmap(dialog.FileName);
                ImageBrush ib = new ImageBrush();
                ImageSourceConverter c = new ImageSourceConverter();

                using (MemoryStream memory = new MemoryStream())
                {
                    bitmap.Save(memory, ImageFormat.Png);
                    memory.Position = 0;
                    BitmapImage bmImage = new BitmapImage();
                    bmImage.BeginInit();
                    bmImage.StreamSource = memory;
                    bmImage.CacheOption = BitmapCacheOption.OnLoad;
                    bmImage.EndInit();

                    ib.ImageSource = bmImage;
                    cvsMap.Width = ib.ImageSource.Width;
                    cvsMap.Height = ib.ImageSource.Height;
                }

                cvsMap.Background = ib;
            }
        }

        #region Check valib input
        private void txtTileWidth_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!char.IsDigit(e.Text, e.Text.Length - 1))
            {
                e.Handled = true;
            }
        }

        private void txtTileHeight_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (!char.IsDigit(e.Text, e.Text.Length - 1))
            {
                e.Handled = true;
            }
        }
        #endregion
        
    }
}
